Beginning Programming with Java for Dummies (ISBN by 0764588745)
Author:0764588745)
Language: eng
Format: mobi
Tags: &NEW
Published: 2011-07-01T01:27:25+00:00
16_588745 ch10.qxd 3/16/05 9:17 PM Page 180
180 Part III: Controlling the Flow
3. In the wizard’s Name field, type the name of your enum type.
To create the code in Listing 10-7, type the name WhoWins.
4. Click Finish.
The wizard disappears to reveal JCreator’s work area. The editor pane has a new WhoWins.java tab. Delete any code that you see in the WhoWins.java pane, and replace that code with the line in Listing 10-7.
(See Figure 10-14.)
Figure 10-14:
Editing the
WhoWins.
java file.
5. Follow the usual steps to add a new Scoreboard class.
In Scoreboard.java file’s editor pane, type the code in Listing 10-8.
6. Choose Build➪Compile Project.
7. Choose Build➪Execute Project.
Voila! The code runs as it does in Figure 10-13.
17_588745 ch11.qxd 3/16/05 9:29 PM Page 181
Chapter 11
How to Flick a Virtual Switch
In This Chapter
ᮣ Dealing with many alternatives
ᮣ Jumping out from the middle of a statement
ᮣ Handling alternative assignments
Imagine playing Let’s Make a Deal with ten different doors. “Choose door number 1, door number 2, door number 3, door number 4. . . . Wait! Let’s break for a commercial. When we come back, I’ll say the names of the other six doors.”
Meet the switch Statement
The code back in Listing 9-2 in Chapter 9 simulates a simple electronic oracle.
Ask the program a question, and the program randomly generates a yes or no answer. But, as toys go, the code in Listing 9-2 isn’t much fun. The code has only two possible answers. There’s no variety. Even the earliest talking dolls could say about ten different sentences.
Suppose that you want to enhance the code of Listing 9-2. The call to myRandom.nextInt(10) + 1 generates numbers from 1 to 10. So maybe you can display a different sentence for each of the ten numbers. A big pile of if statements should do the trick:
if (randomNumber == 1) {
System.out.println(“Yes. Isn’t it obvious?”);
}
if (randomNumber == 2) {
System.out.println(“No, and don’t ask again.”);
}
if (randomNumber == 3) {
System.out.print(“Yessir, yessir!”);
System.out.println(“ Three bags full.”);
}
17_588745 ch11.qxd 3/16/05 9:29 PM Page 182
182 Part III: Controlling the Flow
if (randomNumber == 4)
.
.
.
if (randomNumber < 1 || randomNumber > 10) {
System.out.print(“Sorry, the electronic oracle”);
System.out.println(“ is closed for repairs.”);
}
But that approach seems wasteful. Why not create a statement that checks the value of randomNumber just once and then takes an action based on the value that it finds? Fortunately, just such a statement exists: the switch statement. Listing 11-1 has an example of a switch statement.
Listing 11-1:
An Answer for Every Occasion
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
class TheOldSwitcheroo {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
out.print(“Type your question, my child: “);
myScanner.nextLine();
randomNumber = myRandom.nextInt(10) + 1;
switch (randomNumber) {
case 1:
out.println(“Yes. Isn’t it obvious?”);
break;
case 2:
out.println(“No, and don’t ask again.”);
break;
case 3:
out.print(“Yessir, yessir!”);
out.println(“ Three bags full.”);
break;
case 4:
out.print(“What part of ‘no’”);
out.println(“ don’t you understand?”);
break;
17_588745 ch11.qxd 3/16/05 9:29 PM Page 183
Chapter 11: How to Flick a Virtual Switch
183
case 5:
out.println(“No chance, Lance.”);
break;
case 6:
out.println(“Sure, whatever.”);
break;
case 7:
out.print(“Yes, but only if”);
out.println(“ you’re nice to me.”);
break;
case 8:
out.println(“Yes (as if I care).”);
break;
case 9:
out.print(“No, not until”);
out.println(“ Cromwell seizes Dover.”);
break;
case 10:
out.print(“No, not until”);
out.println(“ Nell squeezes Rover.”);
break;
default:
out.print(“You think you have”);
out.print(“ problems?”);
out.print(“ My random number”);
out.println(“ generator is broken!”);
break;
}
out.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12647)
Hello! Python by Anthony Briggs(9948)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9825)
The Mikado Method by Ola Ellnestam Daniel Brolund(9814)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9719)
Dependency Injection in .NET by Mark Seemann(9369)
Hit Refresh by Satya Nadella(8856)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8334)
The Kubernetes Operator Framework Book by Michael Dame(7945)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7811)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7791)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7735)
Grails in Action by Glen Smith Peter Ledbrook(7722)
Practical Computer Architecture with Python and ARM by Alan Clements(7678)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7645)
Robo-Advisor with Python by Aki Ranin(7630)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7592)
Building Low Latency Applications with C++ by Sourav Ghosh(7521)
Svelte with Test-Driven Development by Daniel Irvine(7499)
